﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Reports.Core
{
    class ProtoGridRowCollection : IReportLayoutRowCollection
    {
        int index = -1;

        List<ReportLayoutRow> list = new List<ReportLayoutRow>();

        public ProtoGridRowCollection(List<ReportLayoutRow> list)
        {
            this.list = list;
        }


        public IReportLayoutRow this[int index]
        {
            get
            {
                if (index < 0 || index > list.Count - 1)
                    return null;

                return list[index];
            }
        }

        public IReportLayoutRow InsertRow(int rowIndex, int height)
        {
            IReportLayoutRow parentrow = this[rowIndex];

            if (parentrow == null)
                return null;

            if (height < 0 || height > parentrow.Height)
                return null;

            ReportLayoutRow row = new ReportLayoutRow(height);

            foreach (IReportLayoutColumn column in parentrow.GetColumns())
            {
                row.GetColumns().AddColumn(column.Width);
            }

            parentrow.SetHeight(parentrow.Height - height);

            list.Insert(rowIndex, row);

            ResetRowIndex();

            return row;
        }

        public IReportLayoutRow InsertRow(int rowIndex)
        {
            IReportLayoutRow parentrow = this[rowIndex];

            if (parentrow == null)
                return null;

            return InsertRow(rowIndex, parentrow.Height / 2);
        }

        public IReportLayoutRow AddRow(int height)
        {
            if (height < 0)
                throw new Exception("height is negative value.");

            ReportLayoutRow row = new ReportLayoutRow(height);

            list.Add(row);

            ResetRowIndex();

            return row;
        }


        public IReportLayoutRow RemoveAt(int index)
        {
            ReportLayoutRow rrow = this[index] as ReportLayoutRow;

            if (rrow == null)
                return null;

            int rowindex = rrow.RowIndex;

            int rowheight = rrow.Height;

            bool islastrow = (rowindex == list.Count - 1);

            if (islastrow)
                return null;

            //remove row

            list.Remove(rrow);

            //set rowindex

            ResetRowIndex();

            //set height

            list[rowindex].SetHeight(list[rowindex].Height + rowheight);

            return rrow;
        }

        public int Count
        {
            get { return list.Count; }
        }

        public void ResetHeight(int maxHeight)
        {
            int orgHeight = 0;

            foreach (IReportLayoutRow row in list)
            {
                orgHeight += row.Height;
            }

            foreach (IReportLayoutRow row in list)
            {
                double newHeight = (double)row.Height * (double)maxHeight / (double)orgHeight;

                row.SetHeight((int)newHeight);
            }
        }

        private void ResetRowIndex()
        {
            int counter = 0;

            foreach (ReportLayoutRow row in list)
            {
                row.RowIndex = counter++;
            }
        }


        #region IEnumerator<IReportLayoutRow> member

        public IReportLayoutRow Current
        {
            get
            {
                return list[index];
            }
        }

        #endregion

        #region IDisposable member

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator member

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public bool MoveNext()
        {
            if (index >= list.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        #region IEnumerable<IReportLayoutRow> member

        public IEnumerator<IReportLayoutRow> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion
    }
}
